package com.xiong.service.impl;

import com.github.pagehelper.PageHelper;
import com.xiong.dao.CourseDao;
import com.xiong.pojo.Course;
import com.xiong.service.CourseService;
import com.xiong.service.StudentCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 熊宇
 * @date 2022/4/29 15:07
 * @description
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseDao courseDao;
    @Autowired
    StudentCourseService studentCourseService;

    @Override
    public Course selectById(Long id) {
        return courseDao.selectById(id);
    }

    @Override
    public Course selectByCourseName(String courseName,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return courseDao.selectByCourseName("%" + courseName + "%");
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteByCourseId(Long id) {
        if(studentCourseService.selectCountByCourseId(id) > 0){
            throw new RuntimeException("学生在使用");
        }
        return courseDao.deleteByCourseId(id);
    }
}
